<?php

class MemberController extends Controller
{
	public $layout='//layouts/user_main';
	/**
	 * 	 * @return array action filters
	 * 	 	 */
	public function filters()
	{
		return array(
				'accessControl', // perform access control for CRUD operations
		);
	}

	/**
	 ** Specifies the access control rules.
	 ** This method is used by the 'accessControl' filter.
	 ** @return array access control rules
	 **/
	public function accessRules()
	{
		return array(
				array('allow',  // allow all users to perform 'index' and 'view' actions
						'actions'=>array('view', 'index'),
						'users'=>array('*'),
				),
				array('allow',  // allow all users to perform 'index' and 'view' actions
						'actions'=>array('profile', 'update', 'myevents', 'password'),
						'users'=>array('@'),
				),
		);
	}

	public function actionIndex($username)
	{
		$this->redirect(array("event/list","username"=>$username));
	}


	public function actionAdmin($username)
	{
		$this->redirect(array("member/update","username"=>$username));
	}
	/**
	 * This method will change user' password.
	 */
	public function actionPassword(){
		$member = Member::model()->findByPk(Yii::app()->user->id);
		if(isset($_POST['Member'])){
			if($_POST['Member']['password'] != $_POST['Member']['password2']){
				Yii::app()->user->setFlash("error","两次新密码输入不一致");
			}else{
				$identity = new UserIdentity(Yii::app()->user->name,$_POST['Member']['oldpassword']);
				if(!$identity->authenticate() ){
					Yii::app()->user->setFlash("error","旧密码错误");
				}else{
					if(strlen($_POST['Member']['password2']) < 7){
						Yii::app()->user->setFlash("error","密码长度必须大于6");
					}else{
						$member->password =hash('md5',$_POST['Member']['password2']);
						if($member->validate() && $member->save()){
							Yii::app()->user->setFlash("success","密码更新成功");
						}else{
							Yii::app()->user->setFlash("error","密码更新失败");
						}
					}
				}
			}
		}

		$this->render('password', array('member'=>$member));
	}


	/**
	 * This method will show user's events.
	 */
	public function actionMyevents(){
		$mid=Yii::app()->user->id;
		$mem=$this->loadMember($mid);
		$this->render('myevents', array('mem'=>$mem));
	}


	/**
	 * This method will update user's information
	 */
	public function actionUpdate($username){
		$this->pageTitle="更新信息 ";
		$member = Member::model()->find("username=:username",array("username"=>$username));
		if(isset($_POST['Member'])){
			$member->attributes = $_POST['Member'];
			$member->status=Member::STATUS_ACTIVE;
			$member->type = Member::TYPE_GROUP;
			$member->avatar = $_POST['Member']['avatar'];
			$member->avatar=CUploadedFile::getInstance($member,'avatar');
			if($member->validate()){
				if($member->save()){
					//upload avatar
					if(!empty($member->avatar)){  //upload event front cover image
						$filepath = Utils::getImagePathById("avatar", "", $member->id);
						$filepath_thumbnail = Utils::getImagePathById("avatar", "thumbnail", $member->id);
						$member->avatar->saveAs($filepath);
						$image = new Image();
						ImageUtils::resize($filepath,$filepath,Constants::$size_avatar_big,Constants::$size_avatar_big,true);
						ImageUtils::resize($filepath,$filepath_thumbnail,Constants::$size_avatar_small,Constants::$size_avatar_small,true);
					}
				}
			}else{
				$this->render('update',array('member'=>$member));
				return;
			}
		}

		//	$user->password=null;
		$this->render('update',array('member'=>$member));
	}

	/**
	 * This method will show user's profile
	 */
	public function actionProfile(){
		$mid=Yii::app()->user->id;
		$mem=$this->loadMember($mid);
		$this->render('profile', array('mem'=>$mem));
	}


	/**
	 * This method will show the user information
	 */
	public function actionView($id){
		$mem=$this->loadMember($id);
		$this->render('view', array('mem'=>$mem));
	}

	/**
	 * This method will load member, and throw Exception if member is not found.
	 */
	private function loadMember($mid){
		$mem=Member::model()->findByPk($mid);
		if($mem==null){
			throw new CHttpException(404,'页面不存在啊.');
		}
		return $mem;
	}

	/**
	 ** Performs the AJAX validation.
	 ** @param CModel the model to be validated
	 **/
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='user-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}



}
